package com.example.mq.mqserver.datacenter;

import com.example.mq.MessageQueueApplication;
import com.example.mq.mqserver.core.Binding;
import com.example.mq.mqserver.core.Exchange;
import com.example.mq.mqserver.core.ExchangeType;
import com.example.mq.mqserver.core.MSGQueue;
import com.example.mq.mqserver.mapper.MetaMapper;

import java.io.File;
import java.util.List;

/*
* 整合数据库操作
* */
public class DataBaseManager {
    private MetaMapper metaMapper;

    // 对数据库进行初始化
    public void init(){
        // 手动获取到 MetaMapper
        metaMapper = MessageQueueApplication.context.getBean(MetaMapper.class);

        if(!checkDBExists()){ // 数据库不存在，就进行建库建表操作
            // 创建数据库 目录
            File dataDir = new File("./data");
            dataDir.mkdirs();
            // 创建数据表
            createTable();
            // 插入默认数据
            createDefaultData();
            System.out.println("[DataBaseManager] 数据库初始化完成!");
        } else {
            // 数据库已存在
            System.out.println("[DataBaseManager] 数据库已经存在!");
        }
    }

    public void deleteDatabase(){
        File file = new File("./data/meta.db");
        boolean ret = file.delete();
        if (ret) {
            System.out.println("[DataBaseManager] 数据库删除成功!");
        } else {
            System.out.println("[DataBaseManager] 数据库删除失败!");
        }

        // 删除目录
        File dataDir = new File("./data");
        boolean retdir = dataDir.delete(); // 删除目录时 需要保证目录时空的
        if (retdir) {
            System.out.println("[DataBaseManager] 数据库目录删除成功!");
        } else {
            System.out.println("[DataBaseManager] 数据库目录删除失败!");
        }

    }


    // 判断数据库是否存在
    private boolean checkDBExists() {

        File file = new File("./data/meta.db");
        if(file.exists()){
            return true;
        }
        return false;
    }

    // 创建默认数据表
    private void createTable() {

        metaMapper.createExchangeTable();
        metaMapper.createQueueTable();
        metaMapper.createBindingTable();
        System.out.println("[DataBaseManager] 创建表完成!");
    }

    // 在 ExchangeTable 中 创建一条默认的 匿名交换机
    private void createDefaultData() {
        Exchange exchange = new Exchange();
        exchange.setName("");
        exchange.setType(ExchangeType.DIRECT);
        exchange.setDurable(true);
        exchange.setAutoDelete(false);
        metaMapper.insertExchange(exchange);
        System.out.println("[DataBaseManager] 创建初始数据完成!");

    }

    // exchangeTable 的查找、插入和删除
     public  List<Exchange> selectAllexchanges(){
        return metaMapper.selectAllexchanges();
     }
     public void insertExchange(Exchange exchange){
         metaMapper.insertExchange(exchange);
     }
    public void deleteExchange(String exchangeName){
        metaMapper.deleteExchange(exchangeName);
    }

    // QueueTable 的查找、插入和删除
    public  void insertQueue(MSGQueue queue){
        metaMapper.insertQueue(queue);
    }
    public void deleteQueue(String queueName){
        metaMapper.deleteQueue(queueName);
    }
   public List<MSGQueue> selectAllQueues(){
        return metaMapper.selectAllQueues();
   }
    // BindingTable 的查找、插入和删除
    public void insertBinding(Binding binding){
        metaMapper.insertBinding(binding);
    }
    public void deleteBinding(Binding binding){
        metaMapper.deleteBinding(binding);
    }
    public List<Binding> selectAllBindings(){
        return metaMapper.selectAllBindings();
    }


}
